//
//  LFBReadPreviousViewController.m
//  ONE
//
//  Created by dllo on 16/6/10.
//  Copyright © 2016年 GroupNine. All rights reserved.
//

#import "LFBReadPreviousViewController.h"
#import "LFBNetTool.h"
#import "LFBReadShortModel.h"
#import "LFBReadSerialModel.h"
#import "LFBReadQuestionModel.h"
#import "LFBReadPreviousCell.h"
#import <UITableView+FDTemplateLayoutCell.h>

@interface LFBReadPreviousViewController () <UITableViewDelegate, UITableViewDataSource>

/** 往期列表视图 */
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation LFBReadPreviousViewController
{
    // 往期列表数据源数组
    NSArray *dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = _period;
    self.edgesForExtendedLayout = UIRectEdgeAll;
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self initData];
    [self createPreviousView];
    [self getData];
}

#pragma mark --初始化数据
- (void)initData
{
    
}

#pragma mark --创建往期列表视图
- (void)createPreviousView
{
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[LFBReadPreviousCell class] forCellReuseIdentifier:kLFBReadPreviousCellID];
    
    _tableView.tableFooterView = [UIView new];
    _tableView.separatorColor = LYLSeparatorColor;
    _tableView.separatorInset = UIEdgeInsetsMake(0, 15, 0, 15);
    [self.view addSubview:_tableView];
    [_tableView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

#pragma mark --网络请求
- (void)getData
{
    switch (_readType) {
        case LYLReadTypeShort:
        {
            [LFBNetTool getNetWithURL:[NSString stringWithFormat:LYLApiReadByMonth, LYLApiEssay, _period] Body:nil Header:nil Response:LFBJSON Success:^(id result) {
                if ([result[@"res"] integerValue] == 0) {
                    NSMutableArray *datas = [NSMutableArray array];
                    NSArray *data = result[@"data"];
                    for (NSDictionary *dic in data) {
                        LFBReadShortModel *shortModel = [[LFBReadShortModel alloc] initWithDic:dic];
                        [datas addObject:shortModel];
                    }
                    dataSource = datas;
                    [_tableView reloadData];
                }
                
            } Failure:^(NSError *error) {
                
            }];
            break;
        }
        case LYLReadTypeSerial:
        {
            [LFBNetTool getNetWithURL:[NSString stringWithFormat:LYLApiReadByMonth, LYLApiSerialContent, _period] Body:nil Header:nil Response:LFBJSON Success:^(id result) {
                if ([result[@"res"] integerValue] == 0) {
                    NSMutableArray *datas = [NSMutableArray array];
                    NSArray *data = result[@"data"];
                    for (NSDictionary *dic in data) {
                        LFBReadSerialModel *serialModel = [[LFBReadSerialModel alloc] initWithDic:dic];
                        [datas addObject:serialModel];
                    }
                    dataSource = datas;
                    [_tableView reloadData];
                }
            } Failure:^(NSError *error) {
                
            }];
            break;
        }
        case LYLReadTypeQuestion:
        {
            [LFBNetTool getNetWithURL:[NSString stringWithFormat:LYLApiReadByMonth, LYLApiQuestion, _period] Body:nil Header:nil Response:LFBJSON Success:^(id result) {
                if ([result[@"res"] integerValue] == 0) {
                    NSMutableArray *datas = [NSMutableArray array];
                    NSArray *data = result[@"data"];
                    for (NSDictionary *dic in data) {
                        LFBReadQuestionModel *questionModel = [[LFBReadQuestionModel alloc] initWithDic:dic];
                        [datas addObject:questionModel];
                    }
                    dataSource = datas;
                    [_tableView reloadData];
                }
            } Failure:^(NSError *error) {
                
            }];
            break;
        }
            
        default:
            break;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [tableView dequeueReusableCellWithIdentifier:kLFBReadPreviousCellID forIndexPath:indexPath];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [tableView fd_heightForCellWithIdentifier:kLFBReadPreviousCellID cacheByIndexPath:indexPath configuration:^(LFBReadPreviousCell *cell) {
        [cell configureViewWithReadModel:dataSource[indexPath.row] type:_readType atIndexPath:indexPath];
    }];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [(LFBReadPreviousCell *)cell configureViewWithReadModel:dataSource[indexPath.row] type:_readType atIndexPath:indexPath];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
